package com.blog.controller.adminmainpage;

import java.io.IOException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.blog.controller.base.BaseServlet;
import com.blog.entity.BloggerEntity;
import com.blog.entity.UserEntity;
import com.blog.service.adminbloggers.AdminBloggerService;
import com.blog.service.frontpage.UserService;
@WebServlet("/adminLogin")
public class AdminLoginServlet extends BaseServlet{

	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//1、获取表单的姓名和密码
		String uname = req.getParameter("uname");
		String pwd = req.getParameter("pwd");
		
		// 定义一个集合保存错误信息
		HashMap<String, Object> ms = new HashMap<>();
		if(uname!=null && !uname.isEmpty()) {
			if(pwd!=null && !pwd.isEmpty()) {
				AdminBloggerService service = new AdminBloggerService();
				BloggerEntity queryUserForLogin = service.queryPasswordForLogin(uname);
				if(queryUserForLogin!=null&& queryUserForLogin.getPassword().equals(pwd)) {
					req.getSession().setAttribute("uname", uname);
					ms.put("error", "");
					redirect("/adminMainPage");
					return;
				}else {
					ms.put("error", "用户名或者密码不正确！");
					redirect("/adminLogin", ms);
					return;
				}
			}else {
				ms.put("error", "密码不能为空！");
				redirect("/adminLogin", ms);
				return;
			}
		}else {
			ms.put("error", "用户名不能为空！");
			redirect("/adminLogin", ms);
			return;
		}
		
	}
	

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		forward("admin/adminlogin");
	}
}
